package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UncenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-06
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UncenterClient uncenterClient;

    //统计某一天的注册人数，生成统计数据
    @Override
    public void registerCount(String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用得到某一天的注册人数
        R r=uncenterClient.countRegister(day);
        Integer countRegister=(Integer)r.getData().get("countRegister");

        //把获取数据添加到数据库中
        StatisticsDaily sta=new StatisticsDaily();
        sta.setRegisterNum(countRegister); //注册人数
        sta.setDateCalculated(day); //统计日期

        //存储随机值
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(sta);
    }

    //图表显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);//选择查询哪一个字段,类似select date_calculated,type(取得是type中的值) from student where ...
        List<StatisticsDaily> staList=baseMapper.selectList(wrapper);

        //因为返回的是有两部分数据：日期 和日期对应的数量
        //由于前端要求数组json结构，对应后端java代码时list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList=new ArrayList<>();
        List<Integer> numDataList=new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for(StatisticsDaily daily:staList){
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            if("login_num".equals(type)){
                numDataList.add(daily.getLoginNum());
            }
            if("register_num".equals(type)){
                numDataList.add(daily.getRegisterNum());
            }
            if("video_view_num".equals(type)){
                numDataList.add(daily.getVideoViewNum());
            }
            if("course_num".equals(type)){
                numDataList.add(daily.getCourseNum());
            }
        }
        //把封装之后的两个list集合放到map当中，在前端中map里面存放的就是json数组
        Map<String,Object> map=new HashMap<>();
        map.put("date_calculated",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
